import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches

ages = ['>=50', '[40,50)', '[30,40)', '[20,30)']
percentages = [12.5, 20.8, 29.2, 37.5]

colors = ['#F7C1CF', '#FFD47F', '#7B92C7', '#ADD9EE']

fig, ax = plt.subplots(figsize=(8, 8))

ring_width = 0.3

directions = [(-70, 90), (-110, 90), (-140, 90), (-180, 90)]

for i, (age, percent, direction, color) in enumerate(zip(ages[::-1], percentages[::-1], directions[::-1], colors[::-1])):
    # 计算内外半径
    inner_radius = (len(ages) - i - 1) * ring_width
    outer_radius = inner_radius + ring_width

    # 获取当前圆环的方向
    theta1, theta2 = direction

    # 创建环形
    arc = mpatches.Wedge(center=(0, 0), r=outer_radius, width=ring_width,
                         theta1=theta1, theta2=theta2,
                         facecolor=color, edgecolor='black')

    # 添加环形到坐标轴
    ax.add_patch(arc)

    # 添加年龄标签
    age_angle = -1
    age_x = outer_radius * np.cos(np.radians(90 - age_angle))-0.05
    age_y = (outer_radius + inner_radius) / 2 * np.sin(np.radians(90 - age_angle))
    ax.text(age_x, age_y, age, ha='center', va='center', rotation=age_angle - 90)

    # 添加占比标签
    percent_angle = theta2
    percent_x = outer_radius * np.cos(np.radians(90 - percent_angle))
    percent_y = outer_radius * np.sin(np.radians(90 - percent_angle))
    ax.text(percent_x, percent_y, f'{percent}%', ha='center', va='center', rotation=percent_angle - 90)

#在内部画一个比内圈圆半径小的空白圆
inner_circle_radius = (len(ages) - 1) * ring_width
smaller_inner_circle = mpatches.Circle((0, 0), inner_circle_radius * 0.15,
                                       facecolor='white', edgecolor='none')
ax.add_patch(smaller_inner_circle)

ax.set_xlim(-1 - outer_radius, 1 + outer_radius)
ax.set_ylim(-1 - outer_radius, 1 + outer_radius)

# 设置坐标轴属性
ax.set_aspect('equal')
ax.axis('off')

#plt.savefig('D:/python charm/01/可视化/第二章/图片/玉块图.png')

# 显示图形
plt.show()